// pages/user/user.js
import { Base } from '../../utils/base.js'
import {hex_md5} from '../../utils/md5.js'
const app = getApp();
let base =new Base();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      userInfo: null,
      isShowPopup: false,//是否显示弹窗
      tip: "",//弹窗显示内容
  },

  onLogin:function(e){
    let that=this;
    let name = e.detail.value.name;
    let password = hex_md5(e.detail.value.password) ;
    if (name == "" ){
      base.showMyTip({ _this: this, title: "账号不能为空" })
      return;
    }
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(name)) {
        base.showMyTip({ _this: this, title: "账号有误" })
        return;
    }
    if (e.detail.value.password == ""){
      base.showMyTip({ _this: this, title: "密码不能为空" })
      return;
    }
    let parames={
      "name": name,
      "password": password
    }
   
    app.loginSystem(parames,res=>{
      console.log(res);
      let message = res.data.message;
      if (res.data.status){
        app.globalData.userInfo = res.data.data;
        wx.setStorageSync('userInfo', res.data.data)
        base.showMyTip({ _this: this, title:"登陆成功"});
      

        setTimeout(() => {
            wx.switchTab({
                url: '/pages/user/user',
                success: function (e) {
                    var page = getCurrentPages().pop();
                    if (page == undefined || page == null) return;
                    page.onLoad();
                   
                } 
            })
        },500);
      
      }else{
        base.showMyTip({ _this: this, title: message })
      }
    });
  },
  //获取购物车数据
  getCart() {
    let that = this;
   
    let userInfo = wx.getStorageSync('userInfo');
    let storeNo = userInfo.storeNo;
    if (userInfo) {
      let parames = {
        "userId": '' + userInfo.id,
        "storeNo": '' + storeNo
      }
      //获取购物车总数
    
      let url = "/ddaigo-platform/cart/get";
        base.commonRequest(url, parames, res => {
          if (res.status) {
            let total = 0;
            res.data.forEach((value, index) => {
              total += parseInt(value.goodsNum);
            })
            wx.showTabBarRedDot({
              index: 1
            })
            //设置购物车右上角红色点
            wx.setTabBarBadge({
              index: 1,
              text: '' + total,
            })
           
          }
        });
     
    
    }

  },
  quitLogin(e){
    console.log("111111111111111111");
    let storeNo = wx.getStorageSync("storeNo");
    wx.switchTab({
      url: '/pages/home/home?storeNo=' + storeNo
    })
   
  },
  onShow() {

   console.log("onShow-----------------------");

  },
  //页面隐藏时执行
  onUnload:function() {
    console.log("onUnload==================");
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      let storeNo = wx.getStorageSync("storeNo");
      wx.switchTab({
        url: '/pages/home/home?storeNo=' + storeNo
      })
    }
    wx.setStorageSync('loginClose',true);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  }
})